package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.SkyAbnornalVo;
import org.dromara.system.domain.bo.SkyAbnornalBo;
import org.dromara.system.service.ISkyAbnornalService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 经营异常和严重违法
 *
 * @author Lion Li
 * @date 2024-09-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/abnornal")
public class SkyAbnornalController extends BaseController {

    private final ISkyAbnornalService skyAbnornalService;

    /**
     * 查询经营异常和严重违法列表
     */
    @SaCheckPermission("system:abnornal:list")
    @GetMapping("/list")
    public TableDataInfo<SkyAbnornalVo> list(SkyAbnornalBo bo, PageQuery pageQuery) {
        return skyAbnornalService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出经营异常和严重违法列表
     */
    @SaCheckPermission("system:abnornal:export")
    @Log(title = "经营异常和严重违法", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SkyAbnornalBo bo, HttpServletResponse response) {
        List<SkyAbnornalVo> list = skyAbnornalService.queryList(bo);
        ExcelUtil.exportExcel(list, "经营异常和严重违法", SkyAbnornalVo.class, response);
    }

    /**
     * 获取经营异常和严重违法详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:abnornal:query")
    @GetMapping("/{id}")
    public R<SkyAbnornalVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(skyAbnornalService.queryById(id));
    }

    /**
     * 新增经营异常和严重违法
     */
    @SaCheckPermission("system:abnornal:add")
    @Log(title = "经营异常和严重违法", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SkyAbnornalBo bo) {
        return toAjax(skyAbnornalService.insertByBo(bo));
    }

    /**
     * 修改经营异常和严重违法
     */
    @SaCheckPermission("system:abnornal:edit")
    @Log(title = "经营异常和严重违法", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SkyAbnornalBo bo) {
        return toAjax(skyAbnornalService.updateByBo(bo));
    }

    /**
     * 删除经营异常和严重违法
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:abnornal:remove")
    @Log(title = "经营异常和严重违法", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(skyAbnornalService.deleteWithValidByIds(List.of(ids), true));
    }
}
